package com.thti.mecool.client.widget;

import java.util.Timer;

import android.app.Activity;
import android.app.ProgressDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

public class ProgressDialogWidget{
	Activity mActivity;
	int dismissTime=60;
	Timer timer;
	ProgressDialog pd;
	boolean autoShow=true;
	public ProgressDialogWidget(Activity mActivity) {
		this.mActivity=mActivity;
		init();
	}

	public ProgressDialogWidget(Activity mActivity,boolean autoShow) {
		this.mActivity=mActivity;
		this.autoShow=autoShow;
		init();
	}
	public boolean isShowing(){
		return pd.isShowing();
	}
	
public void setView(int ViewId) {
	View view=LayoutInflater.from(mActivity).inflate(ViewId, null);
	pd.setView(view);
}
private void init(){
	pd=new ProgressDialog(mActivity);
	timer=new Timer();
	pd.setTitle("正在请求数据");
	pd.setMessage("请稍候……");
	pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
	if(autoShow){
					show();}
}
public void setProgress(int value){
	pd.setProgress(value);
}
public void upProgress(){
	if(pd!=null)
	pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
	pd.setMax(100);
}
public void show() {
	pd.show();
		Thread th=new Thread(new Runnable() {
			
			@Override
			public void run() {
				try {
					Thread.sleep(dismissTime*1000);
					if(pd.isShowing()){
						dismiss();
					}
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		});
		th.start();
}


public void setDismissTime(int dismissTime) {
//	if(dismissTime==0)
//		dismissTime=Integer.MAX_VALUE;
//	else
//	this.dismissTime = dismissTime;
}

public void dismiss(String msg) {
	if(pd.isShowing()){
		pd.dismiss();
	}
	Toast.makeText(mActivity, msg, 3000).show();
}
public void dismiss() {
	if(pd.isShowing()){
		pd.dismiss();
	}}
}
